import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const routes = [
  {
    path: '/home',
    name: 'home',
    meta: {
      requireAuth: true, // 进入该路由需要登录
    },
    component: HomeView,
    children: [
      {
        path: '/bci',
        name: 'bci',
        meta: {
          requireAuth: true, // 进入该路由需要登录
        },
        component: () => import(/* webpackChunkName: "about" */ '../views/Bci.vue')
      },
      {
        path: '/sexci',
        name: 'sexci',
        meta: {
          requireAuth: true, // 进入该路由需要登录
        },
        component: () => import(/* webpackChunkName: "about" */ '../views/Sexci.vue')
      },
      {
        path: '/groupci',
        name: 'groupci',
        meta: {
          requireAuth: true, // 进入该路由需要登录
        },
        component: () => import(/* webpackChunkName: "about" */ '../views/Groupci.vue')
      },
      {
        path: '/saibieci',
        name: 'saibieci',
        meta: {
          requireAuth: true, // 进入该路由需要登录
        },
        component: () => import(/* webpackChunkName: "about" */ '../views/Saibieci.vue')
      },
      {
        path: '/recoedci',
        name: 'recoedci',
        meta: {
          requireAuth: true, // 进入该路由需要登录
        },
        component: () => import(/* webpackChunkName: "about" */ '../views/Recoedci.vue')
      },
      {
        path: '/pcbasicinfo',
        name: 'pcbasicinfo',
        meta: {
          requireAuth: true, // 进入该路由需要登录
        },
        component: () => import(/* webpackChunkName: "about" */ '../views/Pcbasicinfo.vue')
      },
      {
        path: '/unpire',
        name: 'unpire',
        meta: {
          requireAuth: true, // 进入该路由需要登录
        },
        component: () => import(/* webpackChunkName: "about" */ '../views/Unpire.vue')
      },
      {
        path: '/memscore',
        name: 'memscore',
        meta: {
          requireAuth: true, // 进入该路由需要登录
        },
        component: () => import(/* webpackChunkName: "about" */ '../views/Memscore.vue')
      },
      {
        path: '/groupevent',
        name: 'groupevent',
        meta: {
          requireAuth: true, // 进入该路由需要登录
        },
        component: () => import(/* webpackChunkName: "about" */ '../views/Groupevent.vue')
      },
      {
        path: '/reinforeg',
        name: 'reinforeg',
        meta: {
          requireAuth: true, // 进入该路由需要登录
        },
        component: () => import(/* webpackChunkName: "about" */ '../views/Reinforeg.vue')
      },
      {
        path: '/compeunit',
        name: 'compeunit',
        meta: {
          requireAuth: true, // 进入该路由需要登录
        },
        component: () => import(/* webpackChunkName: "about" */ '../views/signup-message/Compeunit.vue')
      },
      {
        path: '/sportsige',
        name: 'sportsige',
        meta: {
          requireAuth: true, // 进入该路由需要登录
        },
        component: () => import(/* webpackChunkName: "about" */ '../views/signup-message/Sportsige.vue')
      },
      {
        path: '/arrange',
        name: 'arrange',
        meta: {
          requireAuth: true, // 进入该路由需要登录
        },
        component: () => import(/* webpackChunkName: "about" */ '../views/Ordergame/Arrange.vue')
      },
      {
        path: '/schedule',
        name: 'schedule',
        meta: {
          requireAuth: true, // 进入该路由需要登录
        },
        component: () => import(/* webpackChunkName: "about" */ '../views/Ordergame/Schedule.vue')
      },
      {
        path: '/gamegroup',
        name: 'gamegroup',
        meta: {
          requireAuth: true, // 进入该路由需要登录
        },
        component: () => import(/* webpackChunkName: "about" */ '../views/Ordergame/Gamegroup.vue')
      },
      {
        path: '/escoreorder',
        name: 'escoreorder',
        meta: {
          requireAuth: true, // 进入该路由需要登录
        },
        component: () => import(/* webpackChunkName: "about" */ '../views/Competigrade/Escoreorder.vue')
      },
      {
        path: '/gamemedal',
        name: 'gamemedal',
        meta: {
          requireAuth: true, // 进入该路由需要登录
        },
        component: () => import(/* webpackChunkName: "about" */ '../views/Competigrade/Gamemedal.vue')
      },
      {
        path: '/teamrank',
        name: 'teamrank',
        meta: {
          requireAuth: true, // 进入该路由需要登录
        },
        component: () => import(/* webpackChunkName: "about" */ '../views/Competigrade/Teamrank.vue')
      },
      {
        path: '/gradesport',
        name: 'gradesport',
        meta: {
          requireAuth: true, // 进入该路由需要登录
        },
        component: () => import(/* webpackChunkName: "about" */ '../views/Competigrade/Gradesport.vue')
      },
      {
        path: '/porecord',
        name: 'porecord',
        meta: {
          requireAuth: true, // 进入该路由需要登录
        },
        component: () => import(/* webpackChunkName: "about" */ '../views/Competigrade/Porecord.vue')
      },
      {
        path: '/all_tound',
        name: 'all_tound',
        meta: {
          requireAuth: true, // 进入该路由需要登录
        },
        component: () => import(/* webpackChunkName: "about" */ '../views/Competigrade/All_tound.vue')
      },
      {
        path: '/totalgrade',
        name: 'totalgrade',
        meta: {
          requireAuth: true, // 进入该路由需要登录
        },
        component: () => import(/* webpackChunkName: "about" */ '../views/Competigrade/Totalgrade.vue')
      },
      {
        path: '/gradeNorn',
        name: 'gradeNorn',
        meta: {
          requireAuth: true, // 进入该路由需要登录
        },
        component: () => import(/* webpackChunkName: "about" */ '../views/Systeminfo/GradeNorn.vue')
      },
      {
        path: '/itemGrade',
        name: 'itemGrade',
        meta: {
          requireAuth: true, // 进入该路由需要登录
        },
        component: () => import(/* webpackChunkName: "about" */ '../views/Systeminfo/ItemGrade.vue')
      },
      {
        path: '/printSample',
        name: 'printSample',
        meta: {
          requireAuth: true, // 进入该路由需要登录
        },
        component: () => import(/* webpackChunkName: "about" */ '../views/Systeminfo/PrintSample.vue')
      },
      {
        path: '/userMessage',
        name: 'userMessage',
        meta: {
          requireAuth: true, // 进入该路由需要登录
        },
        component: () => import(/* webpackChunkName: "about" */ '../views/Systeminfo/UserMessage.vue')
      },
      {
        path: '/archive',
        name: 'archive',
        meta: {
          requireAuth: true, // 进入该路由需要登录
        },
        component: () => import(/* webpackChunkName: "about" */ '../views/Systeminfo/Archive.vue')
      }
    ]
  },
  {
    path: '/pmatch_pmid',
    name: 'pmatch_pmid',
    meta: {
      requireAuth: true, // 进入该路由需要登录
    },
    component: () => import(/* webpackChunkName: "about" */ '../views/Ordergame/Pmatch_pmid.vue')
  },
  {
    path: '/arrangeMatch',
    name: 'arrangeMatch',
    meta: {
      requireAuth: true, // 进入该路由需要登录
    },
    component: () => import(/* webpackChunkName: "about" */ '../views/Ordergame/ArrangeMatch.vue')
  },
  {
    path: '/resultOrderEdit',
    name: 'resultOrderEdit',
    meta: {
      requireAuth: true, // 进入该路由需要登录
    },
    component: () => import(/* webpackChunkName: "about" */ '../views/Competigrade/ResultOrderEdit.vue')
  },
  {
    path: '/',
    name: 'Login',
    component: () => import(/* webpackChunkName: "about" */ '../views/Login.vue')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {  // 设置是否需要登录权限
    // 获取token
    let token = sessionStorage.getItem("stu_token");
    try {
      token = JSON.parse(token);
    } catch (error) {
      token = token;
    }
    if (token) {
      function decodeToken(token) {
        const base64Url = token.split('.')[1];
        const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
        const jsonPayload = decodeURIComponent(atob(base64).split('').map(function (c) {
          return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
        }).join(''));

        return JSON.parse(jsonPayload);
      }
      const decodedToken = decodeToken(token);
      let date = Math.floor(Date.now() / 1000) // 获取当前时间
      if (date > decodedToken.exp) {
        alert('登录已过期，请重新登录');
        sessionStorage.removeItem("stu_token");
        // 重定向到登录界面
        next('/')
      } else {
        next(); // 如果登录没有过期，设置通过
      }
    } else {
      next('/'); // 如果token 没有，跳转到首页或者跳转登录界面
    }
  } else {
    if (sessionStorage.removeItem("stu_token") && to.path == '/') {
      next("/"); // 不需要登录权限的页面，如果用户想登录直接跳转首页
    } else {
      next()
    }
  }

})

export default router 
